package com.ruoyi.web.controller.user;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserLoginRecord;
import com.ruoyi.user.service.UserLoginRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员访问记录Controller
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@RestController
@RequestMapping("/user/record")
public class UserLoginRecordController extends BaseController {
    @Autowired
    private UserLoginRecordService userLoginRecordService;

    /**
     * 查询系统访问记录列表
     */
    @PreAuthorize("@ss.hasPermi('user:record:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody UserLoginRecord userLoginRecord) {
        startPage();
        List<UserLoginRecord> list = userLoginRecordService.selectUserLoginRecordList(userLoginRecord);
        return getDataTable(list);
    }

    /**
     * 导出系统访问记录列表
     */
    @PreAuthorize("@ss.hasPermi('user:record:export')")
    @Log(title = "系统访问记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserLoginRecord userLoginRecord) {
        List<UserLoginRecord> list = userLoginRecordService.selectUserLoginRecordList(userLoginRecord);
        ExcelUtil<UserLoginRecord> util = new ExcelUtil<>(UserLoginRecord.class);
        util.exportExcel(response, list, "系统访问记录数据");
    }

    /**
     * 获取系统访问记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:record:query')")
    @PostMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId) {
        return success(userLoginRecordService.selectUserLoginRecordByRecordId(recordId));
    }

    /**
     * 新增系统访问记录
     */
    @PreAuthorize("@ss.hasPermi('user:record:add')")
    @Log(title = "系统访问记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserLoginRecord userLoginRecord) {
        return toAjax(userLoginRecordService.insertUserLoginRecord(userLoginRecord));
    }

    /**
     * 修改系统访问记录
     */
    @PreAuthorize("@ss.hasPermi('user:record:update')")
    @Log(title = "系统访问记录", businessType = BusinessType.UPDATE)
    @PostMapping("update")
    public AjaxResult update(@RequestBody UserLoginRecord userLoginRecord) {
        return toAjax(userLoginRecordService.updateUserLoginRecord(userLoginRecord));
    }

    /**
     * 删除系统访问记录
     */
    @PreAuthorize("@ss.hasPermi('user:record:remove')")
    @Log(title = "系统访问记录", businessType = BusinessType.DELETE)
    @PostMapping("remove/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        return toAjax(userLoginRecordService.deleteUserLoginRecordByRecordIds(recordIds));
    }
}
